package com.jiufen.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jiufen.Exception.RemainingStudentsException;
import com.jiufen.Mapper.ClazzMapper;
import com.jiufen.Pojo.Clazz;
import com.jiufen.Pojo.ClazzQueryParam;
import com.jiufen.Pojo.PageResult;
import com.jiufen.Service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;
    @Override
    public PageResult<Clazz> getClazzList(ClazzQueryParam clazzQueryParam) {
        //查询基本信息
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        List<Clazz> clazzes = clazzMapper.queryClazzList(clazzQueryParam);
        //处理课程状态
        log.debug("数据库返回课程信息={}", clazzes);
        //遍历list集合处理数据
        /**
         * 注意：班级状态，显示为：未开班、已结课、在读中 这三种。
         * 如果：
         *   - 当前时间 > 结课时间：状态未 已结课。
         *   - 当前时间 < 开课时间：状态未 未开班。
         *   - 否则，就是 在读中。
         */
clazzes.forEach(clazz -> {
    if (LocalDate.now().isAfter(clazz.getEndDate())) {
        clazz.setStatus("已结课");
    } else if (LocalDate.now().isBefore(clazz.getBeginDate())) {
        clazz.setStatus("未开班");
    } else {
        clazz.setStatus("在读中");
    }
});
        Page<Clazz> clazzList1 = (Page<Clazz>) clazzes;
        log.info("总记录数：{}", clazzList1.getTotal());
        log.info("总页数：{}", clazzList1.getPages());
        log.info("当前页：{}", clazzList1.getPageNum());
        log.info("每页记录数：{}", clazzList1.getPageSize());
        log.info("当前页记录数：{}", clazzList1.getResult());
        return new PageResult<Clazz>(clazzList1.getTotal(),clazzList1.getResult());
    }

    @Override
    public List<Clazz> getClazzListNew() {
        List<Clazz> clazzList = clazzMapper.queryClazzListNew();
        return  clazzList;
    }

    @Override
    public Integer saveClass(Clazz clazz) {
        if (clazz != null) {
            clazz.setCreateTime(LocalDateTime.now());
            clazz.setUpdateTime(LocalDateTime.now());
            Integer result = clazzMapper.saveClass(clazz);
            return result;
        }
        return 0;
    }

    @Override
    public Clazz getClassById(Integer id) {
        Clazz clazz = clazzMapper.queryClazzByID(id);
        return clazz;
    }

    @Override
    public Integer updateClass(Clazz clazz) {
        log.debug("正在修改班级信息={}",clazz);
        clazz.setUpdateTime(LocalDateTime.now());
        Integer result = clazzMapper.updateClass(clazz);
        return result;
    }

    @Override
    public Integer deleteClass(Integer id) throws RemainingStudentsException {
        // 查询班级下是否还有人员，如果有不允许删除
        Integer numberPeople = clazzMapper.queryNumberPeople(id);
        if (numberPeople > 0)
        {
            throw new RemainingStudentsException("该班级下还有 " + numberPeople + " 名学生未处理，无法删除班级");
        }
        Integer i = clazzMapper.deleteClass(id);
        return i;
    }
}
